<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class caiji_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '10';
        $wsql = ' where status = 1 ';
        $query = $this->db->get_all("select id,name from " . table("shop") . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $v) {
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('shop') . $wsql);
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function goods()
    {
        $this->display();
    }
    public function goods_collect()
    {
        $url = !empty($_POST['url']) ? trim($_POST['url']) : error_json('采集的地址不能为空');
        $ctype = isset($_POST['ctype']) ? intval($_POST['ctype']) : '1';
        $content = $this->get_con($url, 'utf8');
        if ($ctype == '2') {
            $connnn = 'class="tb-thumb tm-clear">(.*)</ul></div>';
            $cons = $this->collect_do($connnn, $content);
            preg_match_all('#<li class=""><a href="(.*)"><img src="(.*)_60x60q90.jpg"/></a></li>#iUs', $cons[1][0], $result);
            foreach ($result[2] as $v) {
                $dataimg[] = $v;
            }
            $consss = $this->collect_do('<h1 data-spm="(.*)">(.*)</h1><p>(.*)</p></div>', $content);
            $data['title'] = $consss[2][0];
            $data['intro'] = $consss[3][0];
            $data['simg'] = $dataimg;
            $prcon = $this->collect_do('defaultItemPrice":"(.*)"', $content);
            $data['price'] = $prcon[1][0];
            $data['market_price'] = $prcon[1][0];
            $conurl = $this->collect_do('"descUrl":"(.*)"', $content);
            $content2 = $this->get_con('http:' . $conurl[1][0], 'utf8');
            $content2 = ltrim($content2, "var desc='");
            $content2 = rtrim($content2, "';");
            $data['content'] = $content2;
        } elseif ($ctype == '3') {
            $consss = $this->collect_do('<h3 class="tb-main-title"data-title="(.*)">(.*)</h3><p class="tb-subtitle">(.*)</p>', $content);
            $data['title'] = $consss[2][0];
            $data['intro'] = $consss[3][0];
            $connnn = '<div class="tb-pic tb-s50"><a href="(.*)"><img data-src="(.*)_50x50.jpg"/></a></div>';
            $result = $this->collect_do($connnn, $content);
            foreach ($result[2] as $v) {
                $dataimg[] = $v;
            }
            $data['simg'] = $dataimg;
            $prcon = $this->collect_do('<em class="tb-rmb-num">(.*)</em>', $content);
            $data['price'] = $prcon[1][0];
            $data['market_price'] = $prcon[1][0];
            $conurl = $this->collect_do("descUrl(.*)http:'(.*)'(.*)' : '(.*)', counterApi", $content);
            $content2 = $this->get_con('http:' . $conurl[4][0], 'utf8');
            $content2 = ltrim($content2, "var desc='");
            $content2 = rtrim($content2, "';");
            $data['content'] = $content2;
        } else {
            preg_match_all("#TShop.Setup(?:\\()(.*)(?:\\))();</script></div>#i", $content, $result);
            $result = $result[1][0];
            $stitle = $this->collect_do('<p>(.*)</p>', $content);
            $result = rtrim($result, ');})(');
            $code = json_decode($result);
            $info = $code->itemDO;
            $data['title'] = $info->title;
            $data['intro'] = $stitle[1][0];
            $data['simg'] = $code->propertyPics->default;
            $data['price'] = $info->reservePrice;
            $data['market_price'] = $info->reservePrice;
            $conurl = $code->api->httpsDescUrl;
            $content2 = $this->get_con('https:' . $conurl, 'utf8');
            $content2 = ltrim($content2, "var desc='");
            $content2 = rtrim($content2, "';");
            $data['content'] = $content2;
        }
        if (!empty($data['title'])) {
            $data['error'] = '0';
            echo json_encode($data);
        } else {
            error_json('采集错误');
        }
    }
    function collect_save()
    {
        set_time_limit(0);
        if ($_POST) {
            $shop_id = isset($_POST['shop_id']) ? $_POST['shop_id'] : error_json('店铺不能为空');
            $data['title'] = !empty($_POST['title']) ? $_POST['title'] : error_json('标题不能为空');
            $cid = !empty($_POST['cid']) ? $_POST['cid'] : error_json('分类不能为空');
            $data['cid'] = $cid;
            $data['price'] = !empty($_POST['price']) ? $_POST['price'] : error_json('价格不能为空');
            $data['market_price'] = !empty($_POST['market_price']) ? $_POST['market_price'] : error_json('市场价格不能为空');
            $post_img = isset($_POST['goods_img']) ? $_POST['goods_img'] : error_json('最少需要传一张图片');
            foreach ($post_img as $v) {
                $img_url = 'http:' . $v;
                $file = file::filename($img_url);
                $filename = UQCMS_PATH . 'temp/images/' . $file;
                $dfile = curl::downloadFile($img_url, $filename);
                if ($dfile['filename']) {
                    $limg = $this->module('album')->upload_move($dfile['filename'], 'goods', '0', null, $shop_id);
                    $img_arr[] = $limg['name'];
                }
            }
            $data['images'] = $img_arr[0];
            $data['intro'] = isset($_POST['intro']) ? $_POST['intro'] : '';
            $data['shop_id'] = $shop_id;
            $data['brand'] = isset($_POST['brand']) ? $_POST['brand'] : '0';
            $content = isset($_POST['content']) ? $_POST['content'] : '';
            $local_status = isset($_POST['local_status']) ? $_POST['local_status'] : '0';
            if ($local_status) {
                preg_match_all("/<\\s*img\\s+[^>]*?src\\s*=\\s*(\\'|\")(.*?)\\1[^>]*?\\/?\\s*>/i", $content, $result);
                foreach ($result[2] as $v) {
                    $file = file::filename($v);
                    $filename = UQCMS_PATH . 'temp/images/' . $file;
                    $dfile = curl::downloadFile($v, $filename);
                    if ($dfile['filename']) {
                        $conimg = $this->module('album')->upload_move($dfile['filename'], 'goods', '0', null, $shop_id);
                        $content = str_replace($v, @$conimg['name'], $content);
                    }
                }
                $data['content'] = $content;
            } else {
                $data['content'] = $content;
            }
            $data['stock'] = isset($_POST['stock']) ? $_POST['stock'] : '0';
            $data['status'] = isset($_POST['status']) ? $_POST['status'] : '1';
            $data['addtime'] = time();
            $query = $this->db->add(table('goods'), $data);
            $id = $this->db->insert_id();
            for ($i = 0; $i < count($img_arr); $i++) {
                $dataImg['gid'] = $id;
                $dataImg['url'] = $img_arr[$i];
                $dataImg['px'] = '0';
                $dataImg['addtime'] = time();
                $this->db->add(table('goods_img'), $dataImg);
            }
            $attrsql['gid'] = $id;
            $attrsql['gcid'] = $cid;
            $attrsql['attr_id'] = '';
            $this->db->add(table('goods_attr_index'), $attrsql);
            $sku_data['gid'] = $id;
            $sku_data['value'] = '0';
            $sku_data['price'] = $data['price'];
            $sku_data['stock'] = $data['stock'];
            $sku_data['code'] = '';
            $this->db->add(table('goods_sku_index'), $sku_data);
            $this->db->update(table('goods'), array('sku_num' => '0'), 'id = ' . $id);
            right_json();
        } else {
            error_json('提交错误');
        }
    }
    function sku_act()
    {
    }
    public function brand()
    {
        $this->display();
    }
    function brand_post()
    {
        if ($_POST) {
            $name = isset($_POST['title']) ? $_POST['title'] : error_json('品牌不能为空');
            $row = $this->db->get_one("select id,name from " . table('goods_brand') . " where name = '" . $name . "'");
            if (!$row['id']) {
                $setarr['cid'] = isset($_POST['cid']) ? $_POST['cid'] : '0';
                $setarr['name'] = $name;
                $images = isset($_POST['images']) ? $_POST['images'] : '';
                $zip_name = UQCMS_PATH . '/temp/images/' . rand(10000, 9999999) . time() . '.jpg';
                $download_arr = curl::downloadFile($images, $zip_name);
                $img_addr = $download_arr['filename'];
                if (!empty($img_addr)) {
                    $result = $this->module('album')->upload_move($img_addr, 'brand');
                    if ($result['name']) {
                        $setarr['images'] = $result['name'];
                    }
                    $setarr['addtime'] = time();
                    $query = $this->db->add(table('goods_brand'), $setarr);
                    if ($query) {
                        $arr['error'] = '0';
                        echo json_encode($arr);
                    } else {
                        error_json('保存错误');
                    }
                } else {
                    error_json('下载失败');
                }
            } else {
                error_json('已经存在');
            }
        } else {
            error_json('提交错误');
        }
    }
    function brand_get()
    {
        $key = !empty($_POST['key']) ? trim($_POST['key']) : '男装';
        $keyword = @iconv('UTF-8', 'GBK', $key);
        $keyword = urlencode($keyword);
        $url = "https://list.tmall.com/ajax/allBrandShowForGaiBan.htm?t=0&q=" . $keyword . "&sort=s&style=g&from=.list.pc_1_searchbutton&userIDNum=";
        $file = curl::get($url);
        $file = @iconv('GBK', 'UTF-8', $file);
        $file = compress_html($file);
        $result_code = json_decode($file);
        foreach ($result_code as $v) {
            $dt['title'] = $v->title;
            if (!empty($v->img)) {
                $dt['images'] = 'https://img.alicdn.com/bao/uploaded/' . $v->img;
            }
            $data[] = $dt;
            unset($dt);
        }
        echo json_encode($data);
    }
    function get_con($url, $code = '', $referer = null)
    {
        $file = curl::get($url, '', $referer);
        $file = preg_replace("/<\\!--.*?-->/si", "", $file);
        if ($code == 'utf8') {
            $file = @iconv('GBK', 'UTF-8', $file);
        }
        $file = compress_html($file);
        return $file;
    }
    function collect_do($con, $file)
    {
        preg_match_all("#" . $con . "#iUs", $file, $arr);
        unset($arr[0]);
        return $arr;
    }
}